package cn.wuqingna.legendgem.gui

import cn.wuqingna.legendgem.gem.GemManager
import cn.wuqingna.legendgem.gui.gemmanager.*
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import taboolib.module.chat.colored
import taboolib.module.configuration.Configuration
import taboolib.module.nms.getItemTag
import taboolib.module.ui.buildMenu
import taboolib.module.ui.type.Basic
import taboolib.platform.util.isAir

class Gui (val config: Configuration) {

    var inventory: Inventory? = null
    val typeSlot = mutableMapOf<String, MutableSet<Int>>()
    val keyIcons = mutableMapOf<String, ItemStack>()
    var useItem = ItemStack(Material.AIR)
    var luckItem = ItemStack(Material.AIR)
    var protectItem = ItemStack(Material.AIR)
    var luck = 0
    var protective = ""

    init{

        typeSlot["USEITEM"] = mutableSetOf()
        typeSlot["ITEM"] = mutableSetOf()
        typeSlot["GEM"] = mutableSetOf()
        typeSlot["PROTECTIVE"] = mutableSetOf()
        typeSlot["LUCK"] = mutableSetOf()
        buildGui()
    }

    fun open(player: Player){
        player.openInventory(inventory)
    }


    fun buildGui(){
        inventory = buildMenu<Basic> {
            val slotIndex = config.getList("settings.icons")!!.size
            rows(slotIndex)
            title = config.getString("settings.title")!!.colored()
            handLocked(true)


            val st = mutableMapOf<String, String>()
            config.getConfigurationSection("icons")!!.getKeys(false).forEach { key ->
                val section = config.getConfigurationSection("icons.${key}")!!
                val item = SlotType.valueOf(section.getString("slot-type")!!).getItem(section)
                st[key] = section.getString("slot-type")!!
                keyIcons[key] = item
            }
            var index = 0
            config.getStringList("settings.icons").forEach { icon ->
                icon.toCharArray().forEach { char ->
                    set(index, keyIcons[char.toString()]!!)
                    typeSlot[st[char.toString()]]!!.add(index)
                    index++
                }
            }




            onClick {event ->
                event.clickEventOrNull()?.apply {
                    event.isCancelled = true
                    if (event.rawSlot > slotIndex*9) {
                        val item = event.clickEvent().currentItem
                        if (item.isAir){ return@onClick }
                        val tag = item.getItemTag()

                        UseItemManager.clickInventory(event, this@Gui, tag)
                        // 镶嵌
                        EmbedManager.embedManager(event, this@Gui, tag)
                        // 幸运
                        LuckManager.clickInventory(event, this@Gui, tag)
                        // 保护
                        ProtectiveManager.protective(event, this@Gui, tag)

                    }
                    // 宝石
                    if (typeSlot["GEM"]!!.contains(rawSlot)) {
                        TackManager.take(event, this@Gui, )
                    }
                    if (typeSlot["LUCK"]!!.contains(rawSlot)) {
                        LuckManager.luck(event, this@Gui)
                    }
                }
            }
        }
    }

    fun refreshUseItem(player: Player){
        typeSlot["USEITEM"]!!.forEach {
            inventory!!.setItem(it, useItem)
            val gems = GemManager.getItemGems(useItem, player)
            var gemIndex = 0 
            typeSlot["GEM"]!!.forEach { index ->
                inventory!!.setItem(index, null)
            }
            if (gems.size != 0){
                typeSlot["GEM"]!!.forEach { index ->
                    if (gemIndex < gems.size)
                    inventory!!.setItem(index, gems[gemIndex])
                    gemIndex++
                }
            }
        }


    }

}